﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using Microsoft.Office;
using Microsoft.Vbe.Interop;
//using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace lcps.Web
{
    public class ExWord
    {
        public static string ExportToWordExped(string unit, string jdate, string jhnum, string proname, string id, string wl)
        {
            object outname;
            //创建WordApp对象
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            try
            {
                GC.Collect();
                object missing = System.Reflection.Missing.Value;
                object Nothing = System.Reflection.Missing.Value;
               
               
                //创建一个名为WordDoc的文档对象               
                Microsoft.Office.Interop.Word.Document doc;
                object b = System.Web.HttpContext.Current.Server.MapPath("~/template/e.doc");
                doc = WordApp.Documents.Open(ref b, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                find("@unit@", unit, Nothing, WordApp);
                find("@proname@", proname, Nothing, WordApp);
                find("@proname@", proname, Nothing, WordApp);
                find("@id@", id, Nothing, WordApp);
                find("@id@", id, Nothing, WordApp);
                find("@nowdate@", DateTime.Now.ToShortDateString(), Nothing, WordApp);
                find("@nowdate@", DateTime.Now.ToShortDateString(), Nothing, WordApp);
                find("@jdate@", jdate, Nothing, WordApp);
                find("@jdate@", jdate, Nothing, WordApp); 
                find("@jdate@", jdate, Nothing, WordApp);
                find("@jhnum@", jhnum, Nothing, WordApp);
                find("@wl@", wl, Nothing, WordApp);
                outname = System.Web.HttpContext.Current.Server.MapPath("~/out/") + id + "e.doc";
                if (File.Exists(outname.ToString()))
                    File.Delete(outname.ToString());
                doc.SaveAs(ref outname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                doc.Close(ref Nothing, ref Nothing, ref Nothing);


                //关闭WordApp对象
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                WordApp = null;
                doc = null;
                KillWord();
                GC.Collect();



            }
            catch (Exception ex)
            {
                KillWord();
                throw ex;
            }
            return outname.ToString();
        }

        public static string ExportToWordconfirm(string unit, string jdate, string jhnum, string proname, string id, string wl)
          {
              object outname;
              try
              {                  
                      GC.Collect();
                      object missing = System.Reflection.Missing.Value;
                      object Nothing = System.Reflection.Missing.Value;
                      //创建WordApp对象
                      Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();


                      //创建一个名为WordDoc的文档对象
                      //string name = ;
                      Microsoft.Office.Interop.Word.Document doc;
                      object b = System.Web.HttpContext.Current.Server.MapPath("~/template/c.doc");
                     
                          doc = WordApp.Documents.Open(ref b, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                           find("@unit@",unit , Nothing, WordApp);                           
                           find("@proname@", proname, Nothing, WordApp);
                           find("@proname@", proname, Nothing, WordApp);
                           find("@id@", id, Nothing, WordApp);
                           find("@id@", id, Nothing, WordApp);
                           find("@nowdate@", DateTime.Now.ToShortDateString(), Nothing, WordApp);
                           find("@nowdate@", DateTime.Now.ToShortDateString(), Nothing, WordApp);
                           find("@jdate@", jdate, Nothing, WordApp);
                           find("@jdate@", jdate, Nothing, WordApp);
                           find("@jhnum@", jhnum, Nothing, WordApp);
                           find("@wl@", wl, Nothing, WordApp);
                           find("@wl@", wl, Nothing, WordApp);
                          outname = System.Web.HttpContext.Current.Server.MapPath("~/out/") + id + "c.doc";
                          if (File.Exists(outname.ToString()))
                              File.Delete(outname.ToString());
                          doc.SaveAs(ref outname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                          doc.Close(ref Nothing, ref Nothing, ref Nothing);                     

                      //关闭WordApp对象
                      WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                      WordApp = null;
                      doc = null;
                      KillWord();
                      GC.Collect();


                  
              }
              catch (Exception ex)
              {
                  KillWord();
                  throw ex; 
              }
              return outname.ToString();
          }

        private static void find(object str, object replacestr, object Nothing, Microsoft.Office.Interop.Word.Application WordApp)
{
    WordApp.Selection.WholeStory();

    Find find = WordApp.Selection.Find; ;
                       find.ClearFormatting();

                       //object str = "school";
                       //object replacestr = ds.Tables[0].Rows[0]["school"].ToString();
                       find.Execute(ref str, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref replacestr, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                  
}


        private static void KillWord()
        {
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
            { 
                p.Kill(); 
            }
        }
    }
}
